package com.cloud.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloud.commonapi.jsonResult.Result;
import com.cloud.commonapi.model.Goods_info;
import com.cloud.commonapi.model.Goods_order;
import com.cloud.commonapi.model.User_info;
import com.cloud.order.config.order;
import com.cloud.order.service.GoodsOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.*;

/**
 * @author 陈楚城
 * @date 2019/9/30 15:06
 */
@RestController
@RequestMapping("/order/goodorders")
@Api(tags="物品交易订单接口")
public class goodsOrderController {
    @Autowired
    private GoodsOrderService goodsOrderService;
    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("")
    @ApiOperation(value ="物品订单生成",notes ="未测试" )
    public Result add( Goods_order goods_order, HttpSession session){
        //查看用户的信誉积分
        JSONObject jSONObject= restTemplate.getForObject("http://usercenter/usercontroller/userinfo/users/"+(Integer) session.getAttribute("loginId"),JSONObject.class);
        JSONObject  data=jSONObject.getJSONObject("data");
        User_info user_info=JSON.parseObject(data.toString(), User_info.class);
        System.out.println(user_info.getUsername());
        if(user_info.getCredit()<60){
            return Result.build(400,"您的信誉不合格，请快去实名认证吧！");
        }

        if(goods_order.getAccountId().equals(session.getAttribute("loginId"))){
            return Result.build(400,"你不能换取自己发布的物品");
        }
        goods_order.setNumber(order.getnumber());
        goods_order.setAccountId((Integer) session.getAttribute("loginId"));//登录人id、购买人id
        goods_order.setStarttime(new Timestamp(new Date().getTime()));
        goods_order.setStatus(0);

        Goods_info goods_info=new Goods_info();
        goods_info.setId(goods_order.getGoodsId());
        goods_info.setStatus(-1);
        if(goodsOrderService.addorder(goods_order) >0){
            restTemplate.put("http://transaction/transaction/goods/Byorder",goods_info);
            return Result.build(200,"增加成功");
        }
        return Result.build(400,"增加失败");
    }

    @GetMapping("/{id}")
    @ApiOperation(value ="查看物品订单详情",notes ="未测试" )
    public Result getorder(@PathVariable Integer id, HttpSession session){
        Goods_order goods_order=goodsOrderService.getorder(id);
        if(goods_order!=null){
            return Result.buildData(202,"查询成功",goods_order);
        }
        return Result.build(400,"查询失败");
    }

    @GetMapping("/list")
    @ApiOperation(value ="查看购买物品订单列表",notes ="未测试" )
    public Result getorderlist( HttpSession session){
        Integer accountId = (Integer) session.getAttribute("loginId");//登录人id
        List<Goods_order> list=goodsOrderService.list(accountId);
        List<Goods_info> goods_infos=new ArrayList<>();
        List<User_info> user_infos=new ArrayList<>();
        for(Goods_order Goods_order : list){
            JSONObject jSONObject1= restTemplate.getForObject("http://transaction/transaction/goods/ByOredr/"+Goods_order.getGoodsId(),JSONObject.class);
            JSONObject  data1=jSONObject1.getJSONObject("data");
            Goods_info goods_info= JSON.parseObject(data1.toString(), Goods_info.class);
            goods_infos.add(goods_info);
            //卖家
            JSONObject jSONObject= restTemplate.getForObject("http://usercenter/usercontroller/userinfo/users/"+goods_info.getAccountId(),JSONObject.class);
            JSONObject  data=jSONObject.getJSONObject("data");
            User_info user_info= JSON.parseObject(data.toString(), User_info.class);
            user_infos.add(user_info);

        }

        HashMap<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("goods_infos",goods_infos);
        map.put("user_infos",user_infos);
        if(map.size()>0){
            return Result.buildData(202,"查询成功",map);
        }
        return Result.build(400,"查询失败");
    }

    @GetMapping("sell/list")
    @ApiOperation(value ="查看销售物品订单列表",notes ="未测试" )
    public Result getorderlist1( HttpSession session){
        Integer accountId = (Integer) session.getAttribute("loginId");//登录人id
        JSONObject jSONObject= restTemplate.getForObject("http://transaction/transaction/goods/accountIdByorder/list/"+accountId,JSONObject.class);
        //JSONObject  data=jSONObject.getJSONObject("data");
        JSONArray jsonArray=jSONObject.getJSONArray("data");
        if(jsonArray==null)
            return Result.build(400,"没有售出商品");
        String js=jsonArray.toJSONString();
        //找到我发布的商品
        List<Goods_info> goods_infos1= JSON.parseArray(js,Goods_info.class);
        //System.out.println(goods_info.get(0).getGoodsname());

        List<Goods_order> orderlist =  new ArrayList<>();
        List<Goods_info> goods_infos = new ArrayList<>();
        for(Goods_info goods_info:goods_infos1){
            //找到我发布的商品是否有订单
            List<Goods_order> goodsOrders=goodsOrderService.list1(goods_info.getId());
            if(goodsOrders.size()>0){
                 orderlist.addAll(goodsOrders);
                    //有的话把商品信息加入
                     goods_infos.add(goods_info);

            }
        }
        List<User_info> user_infos=new ArrayList<>();
        //查看这些订单是谁买的
        for(Goods_order goods_order:orderlist){
            //user_infos.add()
            JSONObject jSONObject1= restTemplate.getForObject("http://usercenter/usercontroller/userinfo/users/"+goods_order.getAccountId(),JSONObject.class);
            JSONObject  data=jSONObject1.getJSONObject("data");
            User_info user_info= JSON.parseObject(data.toString(), User_info.class);
            user_infos.add(user_info);
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("orderlist",orderlist);
        map.put("user_infos",user_infos);
        map.put("goods_infos",goods_infos);
        return Result.buildData(202,"查询成功",map);

    }


    @PutMapping("")
    @ApiOperation(value ="修改订单状态",notes ="未测试" )
    public Result updateorder(  Goods_order goods_order, HttpSession session){

        if(goodsOrderService.updateorder(goods_order)>0){
            return Result.build(200,"修改成功");
        }
         return Result.build(400,"修改失败");

    }
    @PutMapping("cancel")
    @ApiOperation(value ="取消订单",notes ="未测试" )
    public Result updateorder1(Goods_order goods_order, HttpSession session){

        if(goodsOrderService.updateorder(goods_order)>0){
            System.out.println(goods_order.getGoodsId());
            Goods_info goods_info=new Goods_info();
            goods_info.setId(goods_order.getGoodsId());
            goods_info.setStatus(1);
            restTemplate.put("http://transaction/transaction/goods/Byorder",goods_info);
            return Result.build(200,"修改成功");
        }
        return Result.build(400,"修改失败");

    }
    @PutMapping("sell/confirm")
    @ApiOperation(value ="卖家确认收货",notes ="未测试" )
    public Result updateorder2(  Goods_order goods_order, HttpSession session){

        if(goodsOrderService.getorder(goods_order.getId()).getStatus()==1){
            goods_order.setStatus(-3);
        }else if(goodsOrderService.getorder(goods_order.getId()).getStatus()==-4){
            goods_order.setStatus(2);
        }
        if(goodsOrderService.updateorder(goods_order)>0){
            return Result.build(200,"修改成功");
        }
        return Result.build(400,"修改失败");

    }
    @PutMapping("buy/confirm")
    @ApiOperation(value ="买家确认收货",notes ="未测试" )
    public Result updateorder3(  Goods_order goods_order, HttpSession session){

        if(goodsOrderService.getorder(goods_order.getId()).getStatus()==1){
            goods_order.setStatus(-4);
        }else if(goodsOrderService.getorder(goods_order.getId()).getStatus()==-3){
            goods_order.setStatus(2);
        }
        if(goodsOrderService.updateorder(goods_order)>0){
            return Result.build(200,"修改成功");
        }
        return Result.build(400,"修改失败");

    }
}
